package com.yeyks.rocketmq;

import cn.hutool.json.JSONUtil;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yeyks.common.constants.OrderStatus;
import com.yeyks.common.dto.mq.EsCbCallBackOrderMessage;
import com.yeyks.common.dto.mq.EsUpdateMessage;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.order.dal.dao.OrderHeadMapper;
import com.yeyks.order.dal.domain.OrderHead;
import com.yeyks.search.constants.EsIndexConstant;
import com.yeyks.search.service.hotel.EsImportHotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * CnBooking 订单状态修改延迟队列Listener
 *
 * @author Administrator
 * */

@Slf4j
@Component
public class MessageCnBookingOrderListener implements MessageListener {


    @Autowired
    private OrderHeadMapper orderHeadMapper;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            Long startTime = System.currentTimeMillis();
            byte[] body = message.getBody();
            //获取到接收的消息，由于接收到的是byte数组，所以需要转换成字符串
            String msg = new String(body);
            //处理业务逻辑
            disposeLogic(msg);
            log.info("CnBookingOrderUpdate更新检测：监听到的消息：message = {}", msg);
            Long endTime = System.currentTimeMillis();
            log.info("CnBookingOrderUpdate检测：单次消费耗时：" + (endTime - startTime) / 1000);
        } catch (Exception e) {
            log.error("MessageListener.consume error:" + e.getMessage(), e);
        }
        return Action.CommitMessage;
    }

    /**
     * 处理监听到的房态数据更新，更具类型同步更新es中酒店房态数据
    * @param msg
     */
    private void disposeLogic(String msg) {
        try {
            EsCbCallBackOrderMessage cbCallBackOrderMessage = JSONUtil.toBean(msg, EsCbCallBackOrderMessage.class);
            if (null != cbCallBackOrderMessage) {
                OrderHead orderHead1 = orderHeadMapper.selectOne(new QueryWrapper<OrderHead>()
                        .eq(OrderHead.SOURCE_ORDER_ID, cbCallBackOrderMessage.getSourceOrderId()));
                Date date = DateUtils.afterHoursToDate(orderHead1.getCheckIn(), 18L);
                long nowTime = System.currentTimeMillis();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);

                OrderHead orderHead = new OrderHead();
                if(nowTime >= calendar.getTimeInMillis()){
                    orderHead.setStatus(OrderStatus.living);
                }else{
                    orderHead.setStatus(OrderStatus.preCheckIn);
                }
                orderHead.setRemark(cbCallBackOrderMessage.getRemark());
                int update = orderHeadMapper.update(orderHead,
                        new UpdateWrapper<OrderHead>()
                                .eq(OrderHead.SOURCE_ORDER_ID, cbCallBackOrderMessage.getSourceOrderId()));
                if(update > 0){
                    log.info("CnBooking订单Id为【{}】的状态更新成功为【{}】", cbCallBackOrderMessage.getSourceOrderId(),
                            cbCallBackOrderMessage.getStatus());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("webSocket检测CNBooking订单状态：处理监听消息异常：msg = {}", msg);
        }
    }
}
